package yang.learn.ScalaForTheImpatient

import org.junit.Test

import scala.collection.mutable.ArrayBuffer

/**
  * @ClassName: ch03
  * @Description:
  * @Author: zhangyang (zhangyang04@qiyi.com)
  * @CreateDate: 2019-01-09 21:44
  */
@Test
class ch03 {


  @Test
  def s3_1(): Unit = {
    val nums = new Array[Int](10)
    val str = Array("hello", "world")
    nums.foreach(println)
    str.foreach(println)
    str(1) = "goodbye"
    str.foreach(println)
  }


  @Test
  def s3_2(): Unit = {
    val b = new ArrayBuffer[Int]()
    b += 1
    b += (1, 2, 3, 4)
    b ++= Array(1, 2, 3)
    println(b)
    b.trimEnd(1)
    b.insert(2, 6, 7, 8)
    println(b)
    b.remove(2)
    println(b)
    b.toArray
  }

  @Test
  def s3_3(): Unit = {
    val array = Array(1, 2, 3, 4)
    for (elem <- array) {
      print(elem)
    }
  }

  @Test
  def s3_4(): Unit = {
    val array = Array(1, 2, 3, 4, -1, -2)
    val result1 = array.filter(_ % 2 != 0).map(_ * 2)
    println(result1)
  }
}
